import { actionBuilder } from '../utils';
import { appActions } from '../app';

export const Namespace = 'num';

type State = {
    num: number;
};

type Action = {
    type: string;
    payload?: any;
};

const model = {
    namespace: Namespace,
    state: {
        num: 0,
    },
    reducers: {
        ADD_NUM(state: State, { payload, type }: Action) {
            return {
                ...state,
                num: state.num + 1,
            };
        },
    },
    effects: {
        *addNumAsync({ payload, type }: Action, { put, delay }: any) {
            yield put(appActions.SET_LOADING(true));
            yield delay(1000);
            // yield put({
            //     type: 'num/ADD_NUM',
            // }); # 这样也是可以的，type会自动添加Namespace
            yield put({
                type: 'ADD_NUM',
            });
            yield put(appActions.SET_LOADING(false));
        },
    },
    subscriptions: {
        init({ dispatch, history }, done) {
            return () => {
                console.log('卸载了');
            };
        },
    },
};

export const numActions = actionBuilder(model);

export default model;
